/**
 * 
 */
package SimulLois;

import org.apache.log4j.Logger;

import Jama.CholeskyDecomposition;
import Jama.Matrix;
import SimulLois.interfaces.Lois;

/**
 * @author Propriétaire
 * 
 */
public class Gaussienne implements Lois {
	
	public static Logger	LOG	= Logger.getLogger(Gaussienne.class);
	
	public static Double randGaussienne() {
		
		LOG.info("Invocking gausienne generator for one dimention");
		
		Double u1 = Math.random();
		Double u2 = Math.random();
		return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
	}
	
	public static Double[] randGaussienne(Integer u, Matrix Z, Matrix B) {
		
		LOG.info("Invocking gausienne generator for vector space");
		
		Double[] X = new Double[u];
		Double[] Y = new Double[u];
		for (int i = 0; i < u; i++) {
			X[i] = randGaussienne();
		}
		Matrix L = new CholeskyDecomposition(Z).solve(B);
		for (int i = 0; i < u; i++) {
			Y[i] = 0.0;
			for (int j = 0; j < u; j++) {
				Y[i] += L.get(i, j) * X[j];
			}
		}
		return Y;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see main.java.SimulLois.interfaces.Lois#gen()
	 */
	@Override
	public Double gen() {
		// TODO Auto-generated method stub
		return randGaussienne();
	}
}
